home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bench / proc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-22  |  8.1 KB  |  300 lines

  1. /* 
  2.  * proc.c --
  3.  *
  4.  *    Snapshot the process table before and after a benchmark in order
  5.  *    to account time spent in different processes and the kernel.
  6.  *
  7.  * Copyright 1989 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /a/newcmds/bench/RCS/proc.c,v 1.2 89/02/22 11:37:25 jhh Exp $ SPRITE (Berkeley)";
  19. #endif /* not lint */
  20.  
  21. #include <proc.h>
  22. #include <stdio.h>
  23.  
  24. void DoOneTime();
  25. void DoDeltaTime();
  26.  
  27.  
  28. /*
  29.  *----------------------------------------------------------------------
  30.  *
  31.  * GetProcTable --
  32.  *
  33.  *    Take a snapshot of the process table.
  34.  *
  35.  * Results:
  36.  *    The number of valid entries.
  37.  *
  38.  * Side effects:
  39.  *    Fills in the proc table that is passed in.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. int
  45. GetProcTable(num, pcbs, argStrings)
  46.     int num;                /* Size of tables passed in */
  47.     Proc_PCBInfo *pcbs;            /* Array of PCBs */
  48.     Proc_PCBArgString *argStrings;    /* Array of argument strings */
  49. {
  50.     register int status;
  51.  
  52.     status = Proc_GetPCBInfo(0, num - 1, PROC_MY_HOSTID, sizeof(Proc_PCBInfo),
  53.                 pcbs, argStrings, &num);
  54.     if (status != SUCCESS) {
  55.     fprintf(stderr, "Couldn't read process table: %s\n",
  56.         Stat_GetMsg(status));
  57.     exit(1);
  58.     }
  59.     return(num);
  60. }
  61.  
  62. /*
  63.  *----------------------------------------------------------------------
  64.  *
  65.  * PrintProcStats --
  66.  *
  67.  *    Compute how much time has been spent by all existing processes
  68.  *    between two snapshots.
  69.  *
  70.  * Results:
  71.  *    None.
  72.  *
  73.  * Side effects:
  74.  *    Prints out info.
  75.  *
  76.  *----------------------------------------------------------------------
  77.  */
  78.  
  79. void
  80. PrintProcStats(outStream, numPCB1, pcbs1, argStrings1, numPCB2, pcbs2, argStrings2)
  81.     FILE *outStream;
  82.     int numPCB1;
  83.     Proc_PCBInfo *pcbs1;        /* Initial PCB snapshot */
  84.     Proc_PCBArgString *argStrings1;    /*     and argument strings */
  85.     int numPCB2;
  86.     Proc_PCBInfo *pcbs2;        /* Final PCB snapshot */
  87.     Proc_PCBArgString *argStrings2;    /*     and argument strings */
  88. {
  89.     register Proc_PCBInfo *pcb1Ptr;
  90.     register Proc_PCBInfo *pcb2Ptr;
  91.     register int i;
  92.     Time sumUserTime;
  93.     Time sumKernelTime;
  94.  
  95.     sumUserTime.seconds = 0;
  96.     sumUserTime.microseconds = 0;
  97.     sumKernelTime.seconds = 0;
  98.     sumKernelTime.microseconds = 0;
  99.     /*
  100.      * Loop through final snapshot.  For each entry either subtract
  101.      * out times indicated in the initial snapshot, or print out
  102.      * the total time because the process started after the benchmark did.
  103.      */
  104.     for (i=0, pcb1Ptr = pcbs1, pcb2Ptr = pcbs2 ;
  105.      i<numPCB2 ;
  106.      i++, pcb1Ptr++, pcb2Ptr++) {
  107. #ifdef notdef
  108.     /*
  109.      * HACK BECAUSE proc.h IS CHANGED.
  110.      */
  111.     if (i != 0) {
  112.         pcb1Ptr = (Proc_PCBInfo *)((int)pcb1Ptr - 5 * sizeof(int));
  113.         pcb2Ptr = (Proc_PCBInfo *)((int)pcb2Ptr - 5 * sizeof(int));
  114.     }
  115.     /* END HACK */
  116. #endif notdef
  117.     if (pcb2Ptr->state == PROC_UNUSED) {
  118.         continue;
  119.     }
  120.     if (NoTimeSpent(pcb2Ptr, pcb1Ptr)) {
  121.         continue;
  122.     }
  123.     if (i < numPCB1 && pcb2Ptr->processID == pcb1Ptr->processID) {
  124.         DoDeltaTime(outStream, "U",
  125.         &pcb2Ptr->userCpuUsage,
  126.         &pcb2Ptr->childUserCpuUsage,
  127.         &pcb1Ptr->userCpuUsage,
  128.         &pcb1Ptr->childUserCpuUsage,
  129.         &sumUserTime);
  130.         DoDeltaTime(outStream, "S",
  131.         &pcb2Ptr->kernelCpuUsage,
  132.         &pcb2Ptr->childKernelCpuUsage,
  133.         &pcb1Ptr->kernelCpuUsage,
  134.         &pcb1Ptr->childKernelCpuUsage,
  135.         &sumKernelTime);
  136.     } else {
  137.         DoOneTime(outStream, "U",
  138.         &pcb2Ptr->userCpuUsage,
  139.         &pcb2Ptr->childUserCpuUsage,
  140.         &sumUserTime);
  141.         DoOneTime(outStream, "S",
  142.         &pcb2Ptr->kernelCpuUsage,
  143.         &pcb2Ptr->childKernelCpuUsage,
  144.         &sumKernelTime);
  145.     }
  146.     if (strlen(argStrings2[i].argString) > 58) {
  147.         argStrings2[i].argString[58] = '\0';
  148.     }
  149.     fprintf(outStream, "%s\n", argStrings2[i].argString);
  150.     }
  151.     fprintf(outStream, "Total User %6d.%06d Total Kernel %6d.%06d\n",
  152.         sumUserTime.seconds, sumUserTime.microseconds,
  153.         sumKernelTime.seconds, sumKernelTime.microseconds);
  154. }
  155.  
  156. /*
  157.  *----------------------------------------------------------------------
  158.  *
  159.  * NoTimeSpent --
  160.  *
  161.  *    See if this process spent any time during the benchmark.
  162.  *
  163.  * Results:
  164.  *    TRUE if the process built up NO time.
  165.  *
  166.  * Side effects:
  167.  *    None.
  168.  *
  169.  *----------------------------------------------------------------------
  170.  */
  171. int
  172. NoTimeSpent(pcb2Ptr, pcb1Ptr)
  173.     register Proc_PCBInfo *pcb2Ptr;
  174.     register Proc_PCBInfo *pcb1Ptr;
  175. {
  176.     Time endTime, startTime;
  177.     if (pcb2Ptr->processID == pcb1Ptr->processID) {
  178.     Time_Add(pcb2Ptr->userCpuUsage, pcb2Ptr->childUserCpuUsage,
  179.         &endTime);
  180.     Time_Add(pcb1Ptr->userCpuUsage, pcb1Ptr->childUserCpuUsage,
  181.         &startTime);
  182.     Time_Subtract(endTime, startTime, &endTime);
  183.     if (endTime.seconds + endTime.microseconds != 0) {
  184.         return(0);    /* Have User CPU usage */
  185.     }
  186.     Time_Add(pcb2Ptr->kernelCpuUsage,
  187.         pcb2Ptr->childKernelCpuUsage, &endTime);
  188.     Time_Add(pcb1Ptr->kernelCpuUsage,
  189.         pcb1Ptr->childKernelCpuUsage, &startTime);
  190.     Time_Subtract(endTime, startTime, &endTime);
  191.     if (endTime.seconds + endTime.microseconds != 0) {
  192.         return(0);    /* Have Kernel CPU usage */
  193.     }
  194.     return(1);    /* No time */
  195.     } else {
  196.     Time_Add(pcb2Ptr->userCpuUsage, pcb2Ptr->childUserCpuUsage,
  197.         &endTime);
  198.     Time_Add(pcb2Ptr->kernelCpuUsage,
  199.         pcb2Ptr->childKernelCpuUsage, &startTime);
  200.     Time_Add(startTime, endTime, &endTime);
  201.     if (endTime.seconds + endTime.microseconds != 0) {
  202.         return(0);    /* Have CPU usage */
  203.     }
  204.     return(1);    /* No time */
  205.     }
  206. }
  207.  
  208. /*
  209.  *----------------------------------------------------------------------
  210.  *
  211.  * DoDeltaTime --
  212.  *
  213.  *    This computes the time spent in a process and its children
  214.  *    given the ending and starting times.  It prints the result
  215.  *    and accumulates it in a running total.
  216.  *
  217.  * Results:
  218.  *    Time time computed.
  219.  *
  220.  * Side effects:
  221.  *    Add the time into a total.
  222.  *
  223.  *----------------------------------------------------------------------
  224.  */
  225.  
  226. void
  227. DoDeltaTime(outStream, string, endTimePtr, endChildTimePtr,
  228.         startTimePtr, startChildTimePtr, totalTimePtr)
  229.     FILE *outStream;
  230.     char *string;
  231.     register Time *endTimePtr;
  232.     register Time *endChildTimePtr;
  233.     register Time *startTimePtr;
  234.     register Time *startChildTimePtr;
  235.     register Time *totalTimePtr;
  236. {
  237.     register int seconds;
  238.     register int microseconds;
  239.  
  240.     seconds = endTimePtr->seconds - startTimePtr->seconds +
  241.           endChildTimePtr->seconds - startChildTimePtr->seconds;
  242.     microseconds = endTimePtr->microseconds - startTimePtr->microseconds +
  243.          endChildTimePtr->microseconds - startChildTimePtr->microseconds;
  244.     while (microseconds < 0) {
  245.     microseconds += 1000000;
  246.     seconds -= 1;
  247.     }
  248.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000);
  249.     totalTimePtr->microseconds += microseconds;
  250.     if (totalTimePtr->microseconds > 1000000) {
  251.     totalTimePtr->microseconds -= 1000000;
  252.     seconds += 1;
  253.     }
  254.     totalTimePtr->seconds += seconds;
  255. }
  256.  
  257. /*
  258.  *----------------------------------------------------------------------
  259.  *
  260.  * DoOneTime --
  261.  *
  262.  *    Add a process's time to its children's time, then print the
  263.  *    result and accumulate it into a total.
  264.  *
  265.  * Results:
  266.  *    None.
  267.  *
  268.  * Side effects:
  269.  *    Accumulate the time in a total.
  270.  *
  271.  *----------------------------------------------------------------------
  272.  */
  273.  
  274. void
  275. DoOneTime(outStream, string, endTimePtr, endChildTimePtr, totalTimePtr)
  276.     FILE *outStream;
  277.     char *string;
  278.     register Time *endTimePtr;
  279.     register Time *endChildTimePtr;
  280.     register Time *totalTimePtr;
  281. {
  282.     register int seconds;
  283.     register int microseconds;
  284.  
  285.     seconds = endTimePtr->seconds + endChildTimePtr->seconds;
  286.     microseconds = endTimePtr->microseconds + endChildTimePtr->microseconds;
  287.     if (microseconds > 1000000) {
  288.     microseconds -= 1000000;
  289.     seconds += 1;
  290.     }
  291.     fprintf(outStream, "%s %2d.%03d ", string, seconds, microseconds / 1000);
  292.     totalTimePtr->microseconds += microseconds;
  293.     if (totalTimePtr->microseconds > 1000000) {
  294.     totalTimePtr->microseconds -= 1000000;
  295.     seconds += 1;
  296.     }
  297.     totalTimePtr->seconds += seconds;
  298. }
  299.  
  300.